package cn.elwy.kit.file.dialog;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import cn.elwy.common.util.AssertUtil;
import cn.elwy.kit.file.service.FileRename;

public class FileRenameDialog extends TitleAreaDialog {

  private Text prefixText;
  private Text postfixText;
  private Text filePathText;
  private FileRename fileRename;
  private Text replacePrefixText;
  private Text replacePostfixText;

  /**
   * Create the dialog.
   * @param parentShell
   */
  public FileRenameDialog(Shell parentShell) {
    super(parentShell);
    fileRename = new FileRename();
  }

  /**
   * Create contents of the dialog.
   * @param parent
   */
  @Override
  protected Control createDialogArea(Composite parent) {
    setTitle("重命名文件");
    setMessage("将指定路径下的文件，替换所有前缀和后缀。");
    Composite area = (Composite) super.createDialogArea(parent);
    Composite container = new Composite(area, SWT.NONE);
    GridLayout gl_container = new GridLayout(5, false);
    gl_container.marginTop = 10;
    container.setLayout(gl_container);
    container.setLayoutData(new GridData(GridData.FILL_BOTH));

    Label lblp_1 = new Label(container, SWT.NONE);
    lblp_1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
    lblp_1.setText("前缀(&P)：");

    prefixText = new Text(container, SWT.BORDER);
    prefixText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

    Label replacePrefixLabel = new Label(container, SWT.NONE);
    replacePrefixLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
    replacePrefixLabel.setText("前缀替换为：");

    replacePrefixText = new Text(container, SWT.BORDER);
    replacePrefixText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
    new Label(container, SWT.NONE);

    Label lblp = new Label(container, SWT.NONE);
    lblp.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
    lblp.setText("后缀(&P)：");

    postfixText = new Text(container, SWT.BORDER);
    postfixText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

    Label label = new Label(container, SWT.NONE);
    label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
    label.setText("后缀替换为：");

    replacePostfixText = new Text(container, SWT.BORDER);
    replacePostfixText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
    new Label(container, SWT.NONE);

    Label label_2 = new Label(container, SWT.NONE);
    label_2.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
    label_2.setText("文件路径：");

    filePathText = new Text(container, SWT.BORDER);
    filePathText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 3, 1));

    Button browseButton = new Button(container, SWT.NONE);
    browseButton.addSelectionListener(new SelectionAdapter() {
      @Override
      public void widgetSelected(SelectionEvent e) {
        // 新建文件夹（目录）对话框
        DirectoryDialog folderdlg = new DirectoryDialog(getShell());
        // 设置文件对话框的标题
        folderdlg.setText("文件选择");
        // 设置初始路径
        folderdlg.setFilterPath("SystemDrive");
        // 设置对话框提示文本信息
        folderdlg.setMessage("请选择相应的文件夹");
        // 打开文件对话框，返回选中文件夹目录
        String selectPath = folderdlg.open();
        if (AssertUtil.isNotEmpty(selectPath)) {
          filePathText.setText(selectPath);
        }
      }
    });
    browseButton.setText("浏览(&B)");

    return area;
  }

  /**
   * Create contents of the button bar.
   * @param parent
   */
  @Override
  protected void createButtonsForButtonBar(Composite parent) {
    createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
    createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
  }

  @Override
  protected void okPressed() {
    String prefix = prefixText.getText();
    String replacePrefix = replacePrefixText.getText();
    String postfix = postfixText.getText();
    String replacePostfix = replacePostfixText.getText();
    String filePath = filePathText.getText();
    if (AssertUtil.isNotEmpty(filePath) && (AssertUtil.isNotEmpty(prefix) || AssertUtil.isNotEmpty(postfix))) {
      fileRename.rename(filePath, prefix, replacePrefix, postfix, replacePostfix);
      super.okPressed();
    }
  }

  /**
   * Return the initial size of the dialog.
   */
  @Override
  protected Point getInitialSize() {
    return new Point(500, 270);
  }
}
